package net.sf.nlp.segment;

import java.util.ArrayList;
import java.util.List;

public class SegNode implements Cloneable 
{
	private double value = 1;
	private PosType posType;
	private List<AtomicWord> atomicWords;
	private List<WordItem> wordItems;
	private List<Pos> posList = new ArrayList<Pos>();
	private Pos bestPos;
	private String nodeWord;
	private String aliasNodeWord;
	private boolean deleted = false;
	private boolean customized = false;

	public List<AtomicWord> getAtomicWords() {
		return atomicWords;
	}

	public void setAtomicWords(List<AtomicWord> atomicWords) {
		this.nodeWord = null;
		this.atomicWords = atomicWords;
	}

	public void setWordItems(List<WordItem> wordItems) {
		this.wordItems = wordItems;
	}

	public List<WordItem> getWordItems() {
		return wordItems;
	}

	public List<Pos> getPosList() {
		return posList;
	}

	public void setPosList(List<Pos> posList) {
		this.posList = posList;
	}

	public boolean isDeleted() {
		return deleted;
	}

	public void setDeleted(boolean deleted) {
		this.deleted = deleted;
	}

	public boolean isCustomized() {
		return customized;
	}

	public void setCustomized(boolean customized) {
		this.customized = customized;
	}

	public int getStart() 
	{
		return atomicWords.get(0).getIndex();
	}

	public int getEnd() 
	{
		return atomicWords.get(atomicWords.size() -1).getIndex();
	}

	public double getValue() {
		return value;
	}

	public PosType getPosType() {
		return posType;
	}

	public void setPosType(PosType type) {
		this.posType = type;
	}

	public Pos getBestPos() {
		return bestPos;
	}

	public void setBestPos(Pos bestPos) {
		this.bestPos = bestPos;
	}

	public String getNodeWord()
	{
		if (nodeWord == null)
		{
			StringBuffer sb = new StringBuffer();
			
			for (AtomicWord atomicWord : atomicWords)
				sb.append(atomicWord.getAtomicWord());
			
			nodeWord = sb.toString();
		}
		
		return nodeWord;
	}
	
	public String getAliasNodeWord()
	{
		if (customized)
			return getNodeWord();
		
		if (aliasNodeWord == null)
		{
			StringBuffer sb = new StringBuffer();
			
			for (AtomicWord atomicWord : atomicWords)
				sb.append(atomicWord.getAliasWord());
			
			aliasNodeWord = sb.toString();
		}
		
		return aliasNodeWord;
	}
	
	public boolean isChineseWords(int count)
	{
		for (AtomicWord atomicWord : atomicWords)
		{
			if (!atomicWord.isChinese())
				return false;
			
			count = count - atomicWord.getAtomicWord().length();
			
			if (count == 0)
				return true;
		}
		
		return false;
	}
	
	public void setValue(double value) {
		this.value = value;
	}

	public String toString()
	{
		return toString(true);
	}
	
	public String toString(boolean isNeedPos)
	{
		return getNodeWord() + "/" + getBestPos();
	}
	
	@Override
	public SegNode clone()
	{
		SegNode clonedSegNode = null;
		try 
		{
			clonedSegNode = (SegNode)super.clone();
			
			clonedSegNode.atomicWords = cloneAtomicWords();
			clonedSegNode.posList = clonePosList();
			
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		
		
		return clonedSegNode;
	}
	
	private List<AtomicWord> cloneAtomicWords()
	{
		List<AtomicWord> cloneAtomicWords = new ArrayList<AtomicWord>();
		
		for (AtomicWord atomicWord : atomicWords)
			cloneAtomicWords.add(atomicWord.clone());
		
		return cloneAtomicWords;
	}
	
	private List<Pos> clonePosList()
	{
		List<Pos> clonePosList = new ArrayList<Pos>();
		
		for (Pos pos : posList)
			clonePosList.add(pos.clone());
		
		return clonePosList;
	}
}
